package bean.managed;

import entity.ReservationEntity;
import bean.managed.util.JsfUtil;
import bean.managed.util.PaginationHelper;
import bean.session.PickingOrderEntityFacade;
import bean.session.ReservationEntityFacade;

import entity.CompletedGoodsEntity;
import entity.CompletedGoodsLocationEntity;
import entity.LocationQuantityEntity;
import entity.MasterProductionScheduleMonthlyEntity;
import entity.OutboundCompletedGoodsEntity;
import entity.PickListEntity;
import entity.PickingOrderEntity;
import entity.PlannedReserveEntity;
import entity.ProductEntity;
import entity.SalesOrderEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import managedbean.AbstractBacking;
import org.primefaces.event.CloseEvent;
import type.Month;
import type.PickingOrderStatus;

@ManagedBean(name = "reservationEntityController")
@SessionScoped
public class ReservationEntityController extends AbstractBacking implements Serializable {

    private DataModel salesOrderReservations;
    private ReservationEntity current;
    private DataModel items = null;
    private DataModel reservationItems=null;
    @EJB
    private bean.session.ReservationEntityFacade ejbFacade;
    @EJB
    private PickingOrderEntityFacade pickingOrderEntityFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;
    private String reservePdtName;
    private int reservePdtQuantity;
    @ManagedProperty(value = "#{completedGoodsEntityController}")
    CompletedGoodsEntityController completedGoodsEntityController;
    @ManagedProperty(value = "#{salesOrderEntityController}")
    SalesOrderEntityController salesOrderEntityController;
    @ManagedProperty(value = "#{pickingOrderEntityController}")
    PickingOrderEntityController pickingOrderEntityController;
    @ManagedProperty(value = "#{pickListEntityController}")
    PickListEntityController pickListEntityController;
    
    MasterProductionScheduleMonthlyEntityController masterProductionScheduleMonthlyEntityController;
    private DataModel soReservations;
    private MasterProductionScheduleMonthlyEntity mpsMonthly;
    private int plannedReserveQuantity;
    private int plannedReserveQuantity1;
    private int plannedReserveQuantity2;
    private int plannedReserveQuantity3;
    private int plannedReserveQuantity4;
    private int plannedReserveQuantity5;
    private int plannedReserveQuantity6;
    private int plannedReserveQuantity7;
    private String plannedReserveProductName;

    public ReservationEntityController() {
    }

    public MasterProductionScheduleMonthlyEntityController getMasterProductionScheduleMonthlyEntityController() {
        return masterProductionScheduleMonthlyEntityController;
    }

    public void setMasterProductionScheduleMonthlyEntityController(MasterProductionScheduleMonthlyEntityController masterProductionScheduleMonthlyEntityController) {
        this.masterProductionScheduleMonthlyEntityController = masterProductionScheduleMonthlyEntityController;
        //masterProductionScheduleMonthlyEntityController.setReservationEntityController(this);
    }

    public int getPlannedReserveQuantity1() {
        return plannedReserveQuantity1;
    }

    public void setPlannedReserveQuantity1(int plannedReserveQuantity1) {
        this.plannedReserveQuantity1 = plannedReserveQuantity1;
    }

    public int getPlannedReserveQuantity2() {
        return plannedReserveQuantity2;
    }

    public void setPlannedReserveQuantity2(int plannedReserveQuantity2) {
        this.plannedReserveQuantity2 = plannedReserveQuantity2;
    }

    public int getPlannedReserveQuantity3() {
        return plannedReserveQuantity3;
    }

    public void setPlannedReserveQuantity3(int plannedReserveQuantity3) {
        this.plannedReserveQuantity3 = plannedReserveQuantity3;
    }

    public int getPlannedReserveQuantity4() {
        return plannedReserveQuantity4;
    }

    public void setPlannedReserveQuantity4(int plannedReserveQuantity4) {
        this.plannedReserveQuantity4 = plannedReserveQuantity4;
    }

    public int getPlannedReserveQuantity5() {
        return plannedReserveQuantity5;
    }

    public void setPlannedReserveQuantity5(int plannedReserveQuantity5) {
        this.plannedReserveQuantity5 = plannedReserveQuantity5;
    }

    public int getPlannedReserveQuantity6() {
        return plannedReserveQuantity6;
    }

    public void setPlannedReserveQuantity6(int plannedReserveQuantity6) {
        this.plannedReserveQuantity6 = plannedReserveQuantity6;
    }

    public int getPlannedReserveQuantity7() {
        return plannedReserveQuantity7;
    }

    public void setPlannedReserveQuantity7(int plannedReserveQuantity7) {
        this.plannedReserveQuantity7 = plannedReserveQuantity7;
    }

    public String getPlannedReserveProductName() {
        return plannedReserveProductName;
    }

    public void setPlannedReserveProductName(String plannedReserveProductName) {
        this.plannedReserveProductName = plannedReserveProductName;
    }

    public int getPlannedReserveQuantity() {
        return plannedReserveQuantity;
    }

    public void setPlannedReserveQuantity(int plannedReserveQuantity) {
        System.out.println(plannedReserveQuantity + "Planned reserve quantity");
        this.plannedReserveQuantity = plannedReserveQuantity;
    }

    public MasterProductionScheduleMonthlyEntity getMpsMonthly() {
        return mpsMonthly;
    }

    public void setMpsMonthly(MasterProductionScheduleMonthlyEntity mpsMonthly) {
        System.out.println(mpsMonthly.getMonthOfProduction() + "Month of production");
        this.mpsMonthly = mpsMonthly;
    }

    public ReservationEntity getCurrent() {
        return current;
    }

    public void setCurrent(ReservationEntity current) {
        this.current = current;
    }

    public String showReservations() {
        this.soReservations = getFacade().findSOReservations(getSalesOrderEntityController().getSelected().getId());
        return null;
    }

    public DataModel getSoReservations() {
        soReservations = getFacade().findSOReservations(getSalesOrderEntityController().getSelected().getId());
        return soReservations;
    }

    public void setSoReservations(DataModel soReservations) {
        this.soReservations = soReservations;
    }

    public void unreserve() {
        System.out.println(current.getId());
        System.out.println(this.getSelected().getId());

        current.setArchived(true);
        current.getPickingOrder().setArchived(true);
        //current.getSalesOrder().getReservations().remove(this.getSelected());
        getSalesOrderEntityController().getSelected().getPickList().getPickingOrders().remove(this.getSelected().getPickingOrder());
        this.getPickingOrderEntityController().getEjbFacade().edit(this.getSelected().getPickingOrder());
        this.getFacade().edit(current);

        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);

    }
    
      public void unreservePlanned() {
        System.out.println(current.getId());
        System.out.println(this.getSelected().getId());

        current.setArchived(true);
//        current.getPickingOrder().setArchived(true);
        //current.getSalesOrder().getReservations().remove(this.getSelected());
//        getSalesOrderEntityController().getSelected().getPickList().getPickingOrders().remove(this.getSelected().getPickingOrder());
//        this.getPickingOrderEntityController().getEjbFacade().edit(this.getSelected().getPickingOrder());
        this.getFacade().edit(current);

        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);

    }

    public void fulfilPickingOrder() {
        try {
            
            System.out.println(this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getSelected().getQuantity());
            LocationQuantityEntity lqe = this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getSelected();
            System.out.println("toremovecase " + this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getToRemoveCase());
            lqe.setQuantity(lqe.getQuantity() - this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getToRemoveCase());
            CompletedGoodsLocationEntity CGLocationEntity = (CompletedGoodsLocationEntity) lqe.getLocation();
            CGLocationEntity.setUsageCase(CGLocationEntity.getUsageCase() - this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getToRemoveCase());
            this.getCompletedGoodsEntityController().getCompletedGoodsLocationEntityController().getFacade().edit(CGLocationEntity);
            if (lqe.getQuantity() == 0) {
                lqe.setArchived(true);
            }
            System.out.println("SalesOrderID" + this.getSalesOrderEntityController().getSelected().getId());

            //System.out.println("SOReservationID"+SOreservations.get(0).getId());
            //System.out.println(this.getSalesOrderEntityController().getSelected().getReservations().iterator());
            List<ReservationEntity> reservations = new ArrayList(this.getSalesOrderEntityController().getSelected().getReservations());

//        System.out.println("Reservations size"+reservations.get(1).getId()); 
            for (ReservationEntity reservation : reservations) {
                if (reservation.isArchived() == false) {
                    if (reservation.getPickingOrder().equals(this.getPickingOrderEntityController().getSelected())) {
                        reservation.setQuantity(reservation.getQuantity() - this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getToRemoveCase());
                        getEjbFacade().edit(reservation);
                    }
                }
            }

            this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getEjbFacade().edit(lqe);

            OutboundCompletedGoodsEntity outboundCG = new OutboundCompletedGoodsEntity();
            outboundCG.setBatch(lqe.getInventory().getBatch());
            outboundCG.setType("Picked");
            outboundCG.setQuantityRemoved(this.getCompletedGoodsEntityController().getLocationQuantityEntityController().getToRemoveCase());
            this.getCompletedGoodsEntityController().getOutboundCompletedGoodsEntityController().getEjbFacade().create(outboundCG);

            this.getCompletedGoodsEntityController().getLocationQuantityEntityController().setToRemoveCase(0);
//            this.getCompletedGoodsEntityController().setPickingOrderEntity(null);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void plannedReserveCG() {
        PlannedReserveEntity plannedReserve = new PlannedReserveEntity();
        getFacade().create(plannedReserve);
        plannedReserve.setArchived(false);
        plannedReserve.setPlanned(true);
        plannedReserve.getPickingOrder().setArchived(true);

        if (reservePdtName.equals("Classic Nutri Bar (Nuts)")) {
            plannedReserve.setQuantity(this.plannedReserveQuantity + this.plannedReserveQuantity4);
            this.mpsMonthly.setReservedClassicNuts(this.mpsMonthly.getReservedClassicNuts()+this.plannedReserveQuantity + this.plannedReserveQuantity4);
        } else if (reservePdtName.equals("Classic Nutri Bar (Fruits)")) {
            plannedReserve.setQuantity(this.plannedReserveQuantity1 + this.plannedReserveQuantity5);
                 this.mpsMonthly.setReservedClassicFruits(this.mpsMonthly.getReservedClassicFruits()+this.plannedReserveQuantity1 + this.plannedReserveQuantity5);
       
        } else if (reservePdtName.equals("Premium Power Bar (Chocolate Nuts)")) {
            plannedReserve.setQuantity(this.plannedReserveQuantity2 + this.plannedReserveQuantity6);
           this.mpsMonthly.setReservedPremiumNuts(this.mpsMonthly.getReservedPremiumNuts()+this.plannedReserveQuantity2 + this.plannedReserveQuantity6);
       
        } else if (reservePdtName.equals("Premium Power Bar (Redcurrant Fruits)")) {
            plannedReserve.setQuantity(this.plannedReserveQuantity3 + this.plannedReserveQuantity7);
               this.mpsMonthly.setReservedPremiumFruits(this.mpsMonthly.getReservedPremiumFruits()+this.plannedReserveQuantity3 + this.plannedReserveQuantity7);
       
        }


       
        plannedReserve.setMonthOfProduction(this.mpsMonthly.getMonthOfProduction());
        plannedReserve.setYearOfProduction(this.mpsMonthly.getYearOfProduction());
        plannedReserve.setAmountFulfilled(0);

        CompletedGoodsEntity product = this.getCompletedGoodsEntityController().getEjbFacade().findCGByName(reservePdtName);
        plannedReserve.setProduct(product);
        product.getReservations().add(plannedReserve);

//        plannedReserve.getPickingOrder().setProduct(product);
//        product.getPickingOrder().add(plannedReserve.getPickingOrder());


//        plannedReserve.getPickingOrder().setReservation(plannedReserve);
//        plannedReserve.setPickingOrder(plannedReserve.getPickingOrder());

//        plannedReserve.getPickingOrder().setStatus(PickingOrderStatus.draft);

        getSalesOrderEntityController().getSelected().getPlannedReservation().add(plannedReserve);

//        if (getSalesOrderEntityController().getSelected().getPickList() == null) {
//            PickListEntity pickList = new PickListEntity();
//            pickList.setSalesOrder(getSalesOrderEntityController().getSelected());
//             this.getPickListEntityController().getEjbFacade().create(pickList);
//            getSalesOrderEntityController().getSelected().setPickList(pickList);
//            pickList.setSalesOrder(getSalesOrderEntityController().getSelected());
//        }
//        plannedReserve.getPickingOrder().setPickList(getSalesOrderEntityController().getSelected().getPickList());

//        getSalesOrderEntityController().getSelected().getPickList().getPickingOrders().add(plannedReserve.getPickingOrder());

        plannedReserve.setSalesOrder(getSalesOrderEntityController().getSelected());
        plannedReserve.getSalesOrder().getPlannedReservation().add(plannedReserve);
        getSalesOrderEntityController().getEjbFacade().edit(plannedReserve.getSalesOrder());
        this.getFacade().edit(plannedReserve);
        this.getMasterProductionScheduleMonthlyEntityController().getEjbFacade().edit(mpsMonthly);

        this.plannedReserveQuantity = 0;
        this.plannedReserveQuantity1 = 0;
        this.plannedReserveQuantity2= 0;
        this.plannedReserveQuantity3= 0;
        this.plannedReserveQuantity4= 0;
        this.plannedReserveQuantity5= 0;
        this.plannedReserveQuantity6= 0;
        this.plannedReserveQuantity7= 0;
        this.reservePdtName = null;
        
         FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }
    
    public void refreshPage(CloseEvent event){
         FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }

    public void reserveCG() {
        //ReservationEntity reservationEntity = new ReservationEntity();
//        current = this.getSelected();
//        if(current.isArchived()==true){
        ReservationEntity currentReserve = new ReservationEntity();
        getFacade().create(currentReserve);
        currentReserve.setArchived(false);
        currentReserve.setPlanned(false);
//        }
        //getFacade().create(current);
//        System.out.println("In reserve CG");
        CompletedGoodsEntity product = getCompletedGoodsEntityController().getEjbFacade().findCGByName(reservePdtName);
        currentReserve.setProduct(product);
        product.getReservations().add(currentReserve);

        currentReserve.setQuantity(reservePdtQuantity);

        currentReserve.getPickingOrder().setProduct(product);
       
        if(this.getSalesOrderEntityController().getSelected().getPurchaseOrder().getPreferredLeadTime()==null){
        currentReserve.getPickingOrder().setPickDate
                (this.getSalesOrderEntityController().getSelected().getPurchaseOrder().getSalesQuotation().getIndicativeLeadTime());
        this.pickingOrderEntityFacade.scheduleTimer(this.getSalesOrderEntityController().getSelected().getPurchaseOrder().getSalesQuotation().getIndicativeLeadTime(), currentReserve.getPickingOrder().getId());
        
        }
        else{
            currentReserve.getPickingOrder().setPickDate
                (this.getSalesOrderEntityController().getSelected().getPurchaseOrder().getPreferredLeadTime());
            this.pickingOrderEntityFacade.scheduleTimer(this.getSalesOrderEntityController().getSelected().getPurchaseOrder().getPreferredLeadTime(), currentReserve.getPickingOrder().getId());
        }
        product.getPickingOrder().add(currentReserve.getPickingOrder());

        currentReserve.getPickingOrder().setReservation(currentReserve);
        currentReserve.setPickingOrder(currentReserve.getPickingOrder());

        currentReserve.getPickingOrder().setStatus(PickingOrderStatus.draft);

        getSalesOrderEntityController().getSelected().getReservations().add(currentReserve);

        if (getSalesOrderEntityController().getSelected().getPickList() == null) {
            PickListEntity pickList = new PickListEntity();
            pickList.setSalesOrder(getSalesOrderEntityController().getSelected());
            this.getPickListEntityController().getEjbFacade().create(pickList);
            getSalesOrderEntityController().getSelected().setPickList(pickList);
            pickList.setSalesOrder(getSalesOrderEntityController().getSelected());
        }
        currentReserve.getPickingOrder().setPickList(getSalesOrderEntityController().getSelected().getPickList());
        System.out.println(currentReserve.getPickingOrder().getId());


        getSalesOrderEntityController().getSelected().getPickList().getPickingOrders().add(currentReserve.getPickingOrder());

        currentReserve.setSalesOrder(getSalesOrderEntityController().getSelected());
        currentReserve.getSalesOrder().getReservations().add(current);
        getSalesOrderEntityController().getEjbFacade().edit(currentReserve.getSalesOrder());
        this.getFacade().edit(currentReserve);
        //this.getFacade().edit(current);
        this.setReservePdtName(null);
        this.setReservePdtQuantity(0);
        //this.getCompletedGoodsEntityController().getEjbFacade().edit(product);
        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }

    public PickListEntityController getPickListEntityController() {
        return pickListEntityController;
    }

    public void setPickListEntityController(PickListEntityController pickListEntityController) {
        this.pickListEntityController = pickListEntityController;
    }

    public SalesOrderEntityController getSalesOrderEntityController() {
        return salesOrderEntityController;
    }

    public void setSalesOrderEntityController(SalesOrderEntityController salesOrderEntityController) {
        this.salesOrderEntityController = salesOrderEntityController;
    }

    public PickingOrderEntityController getPickingOrderEntityController() {
        return pickingOrderEntityController;
    }

    public void setPickingOrderEntityController(PickingOrderEntityController pickingOrderEntityController) {
        this.pickingOrderEntityController = pickingOrderEntityController;
    }

    public int getReservePdtQuantity() {
        return reservePdtQuantity;
    }

    public void setReservePdtQuantity(int reservePdtQuantity) {
        this.reservePdtQuantity = reservePdtQuantity;
    }

    public String getReservePdtName() {
        return reservePdtName;
    }

    public void setReservePdtName(String reservePdtName) {
        System.out.println(reservePdtName);
        this.reservePdtName = reservePdtName;
    }

    public ReservationEntity getSelected() {
        if (current == null) {
            current = new ReservationEntity();
            selectedItemIndex = -1;
        }
        return current;
    }

    public CompletedGoodsEntityController getCompletedGoodsEntityController() {
        return completedGoodsEntityController;
    }

    public void setCompletedGoodsEntityController(CompletedGoodsEntityController completedGoodsEntityController) {
        this.completedGoodsEntityController = completedGoodsEntityController;
        this.completedGoodsEntityController.setReservationEntityController(this);
    }

    private ReservationEntityFacade getFacade() {
        return ejbFacade;
    }

    public ReservationEntityFacade getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(ReservationEntityFacade ejbFacade) {
        this.ejbFacade = ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "/entities/reservationEntity/List?faces-redirect=true";
    }

    public String prepareView() {
        current = (ReservationEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "View";
    }

    public String prepareCreate() {
        current = new ReservationEntity();
        selectedItemIndex = -1;
        return "Create";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ReservationEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        current = (ReservationEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ReservationEntityUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        current = (ReservationEntity) getItems().getRowData();
        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        performDestroy();
        recreateModel();
        return "List";
    }

    public String destroyAndView() {
        performDestroy();
        recreateModel();
        updateCurrentItem();
        if (selectedItemIndex >= 0) {
            return "View";
        } else {
            // all items were removed - go back to list
            recreateModel();
            return "List";
        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ReservationEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void updateCurrentItem() {
        int count = getFacade().count();
        if (selectedItemIndex >= count) {
            // selected index cannot be bigger than number of items:
            selectedItemIndex = count - 1;
            // go to previous page if last page disappeared:
            if (pagination.getPageFirstItem() >= count) {
                pagination.previousPage();
            }
        }
        if (selectedItemIndex >= 0) {
            current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
        }
    }

    public DataModel getItems() {
        if(this==null){
            System.out.println("FUCK");
        }
         if(this.ejbFacade==null){
            System.out.println("FUCKfuckfuck");
        }
     items= new ListDataModel(this.ejbFacade.findAll());
        return items;
    }
    
     public DataModel getReservationItems() {
            if(this.ejbFacade==null){
            System.out.println("FUCKfuckfuck");
        }
     reservationItems= new ListDataModel(this.ejbFacade.findReservation());
        return reservationItems;
    }

    private void recreateModel() {
        items = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        return "List";
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        return "List";
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = ReservationEntity.class)
    public static class ReservationEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ReservationEntityController controller = (ReservationEntityController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "reservationEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof ReservationEntity) {
                ReservationEntity o = (ReservationEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ReservationEntityController.class.getName());
            }
        }
    }
}
